<template>
  <div class="attendance">
    <module
      ref="module"
      :borderConfig="attendanceMonth.borderConfig"
      :moduleConfig="attendanceMonth.moduleConfig"></module>
    <module
      ref="module1"
      :borderConfig="leaveMonth.borderConfig"
      :moduleConfig="leaveMonth.moduleConfig"></module>
    <module
      ref="module2"
      :borderConfig="attendanceOrgan.borderConfig"
      :moduleConfig="attendanceOrgan.moduleConfig"
      :dimensionArr="attendanceOrgan.dimensionArr"
      :isShowDimension="attendanceOrgan.isShowDimension"></module>
    <module
      ref="module3"
      :borderConfig="leaveOrgan.borderConfig"
      :dimensionArr="leaveOrgan.dimensionArr"
      :isShowDimension="leaveOrgan.isShowDimension"
      :moduleConfig="leaveOrgan.moduleConfig"></module>
  </div>
</template>

<script>
import module from '../components/SecondaryModule'
import { changeBarData, separateData } from './../../utils/index'
export default {
  components: {
    module
  },
  computed: {
    organId () {
      return this.$store.state.organId
    }
  },
  name: 'attendance',
  data () {
    return {
      // 全勤率趋势配置
      attendanceMonth: {
        borderConfig: {
          titleText: '全勤率趋势',
          moduleType: '2x32',
          ref: 'attendanceMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: '%', // Y轴坐标%
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: false,
              tipsMessage: '全勤率',
              tipsColor: ['#f29f46', '#b60d81']
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 2 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 请假天数趋势配置
      leaveMonth: {
        borderConfig: {
          titleText: '请假天数趋势',
          moduleType: '2x32',
          ref: 'leaveMonth'
        },
        moduleConfig: {
          optionsConfig: {
            title: {
              text: '天',
              textStyle: {
                color: '#7aabe7'
              },
              top: 5,
              left: 30
            },
            axis: {
              // yAxisConversion: '%', // Y轴坐标%
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              // symbol: '%',
              isNamePreposition: false,
              tipsMessage: '请假天数'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 全勤率对比配置
      attendanceOrgan: {
        borderConfig: {
          titleText: '全勤率对比',
          moduleType: '2x32',
          ref: 'attendanceOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              xAxisNameLength: 8,
              yAxisConversion: '%' // Y轴坐标%
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: false,
              tipsMessage: '全勤率'
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              right: '10%'
            },
            seriesData: []
          }
        },
        isShowDimension: true,
        dimensionArr: ['本月']
      },
      // 请假天数对比配置
      leaveOrgan: {
        borderConfig: {
          titleText: '请假天数对比',
          moduleType: '2x32',
          ref: 'leaveOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            title: {
              text: '天',
              textStyle: {
                color: '#7aabe7'
              },
              top: 5,
              left: 30
            },
            axis: {
              xAxisNameLength: 8
              // yAxisConversion: '%' // Y轴坐标%
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              // symbol: '%',
              isNamePreposition: false,
              tipsMessage: '请假天数'
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              right: '10%'
            },
            seriesData: []
          }
        },
        isShowDimension: true,
        dimensionArr: ['本月']
      }
    }
  },
  methods: {
    showTips (data) {
      this.$parent.showTips(data)
    },
    // 改变时间维度时
    changeDimension (obj) {
      switch (obj.ref) {
        case 'attendanceOrgan': this.getBranchNormalRate(obj.index + 1)
          break
        case 'leaveOrgan': this.getBranchLeaveDay(obj.index + 1)
          break
        default: break
      }
    },
    // 全勤率趋势
    getNormalRate () {
      this.staffApi.getNormalRate({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data, 'normalRate')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.attendanceMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 2,
            isBar: true
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 1,
            isBar: true
          }
        ]
      }, res => {
        this.attendanceMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 2,
            isBar: true
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 1,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 请假天数趋势
    getLeaveDay () {
      this.staffApi.getLeaveDay({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data, 'count')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.leaveMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 1,
            isBar: true
          }
        ]
      }, res => {
        this.leaveMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 1,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 全勤率对比
    getBranchNormalRate (timeDimensionIndex) {
      let timeDimension = 1
      if (timeDimensionIndex) {
        timeDimension = timeDimensionIndex
      }
      this.staffApi.getBranchNormalRate({ organId: this.organId, timeDimension: timeDimension }).then(res => {
        let data = []
        let name = []
        if (res.data.code === '0') {
          let obj = separateData(res.data.data, 'normalRate')
          data = obj.data
          name = obj.tipsName
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.attendanceOrgan.moduleConfig.optionsConfig.xAxis.data = name
        this.attendanceOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: data,
            colorIndex: 0,
            isBar: true
          }
        ]
      }, res => {
        this.attendanceOrgan.moduleConfig.optionsConfig.xAxis.data = name
        this.attendanceOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            colorIndex: 0,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 请假天数对比
    getBranchLeaveDay (timeDimensionIndex) {
      let timeDimension = 1
      if (timeDimensionIndex) {
        timeDimension = timeDimensionIndex
      }
      this.staffApi.getBranchLeaveDay({ organId: this.organId, timeDimension: timeDimension }).then(res => {
        let data = []
        let name = []
        if (res.data.code === '0') {
          let obj = separateData(res.data.data, 'count')
          data = obj.data
          name = obj.tipsName
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.leaveOrgan.moduleConfig.optionsConfig.xAxis.data = name
        this.leaveOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: data,
            colorIndex: 0,
            isBar: true
          }
        ]
      }, res => {
        this.leaveOrgan.moduleConfig.optionsConfig.xAxis.data = []
        this.leaveOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            colorIndex: 0,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 请求数据
    getData () {
      this.getNormalRate()
      this.getLeaveDay()
      this.getBranchNormalRate()
      this.getBranchLeaveDay()
    },
    resizeChart () {
      this.$refs.module.resizeChart()
      this.$refs.module1.resizeChart()
      this.$refs.module2.resizeChart()
      this.$refs.module3.resizeChart()
    }
  },
  mounted () {
    // this.getData()
  }
}
</script>

<style lang="scss" scoped>
  .attendance{
    width: calc(100% - 10px);
    position: relative;
    margin: 5px;
    overflow: hidden;
    moz-user-select: -moz-none;
    -moz-user-select: none;
    -o-user-select:none;
    -khtml-user-select:none;
    -webkit-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }
</style>
